#include "news_entry.h"

#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>

#include <iostream>
#include <utility>

using namespace std;

NewsEntry::NewsEntry(string url_, string body_) : url(url_), body(body_)
{
	fillBadWords();
	countWordsInBody();
}

void NewsEntry::countWordsInBody()
{
	istringstream iss(body);
	do
    {
        string sub;
        iss >> sub;
		if (sub.size()<4 || isBadWord(sub)) continue;
		words[sub] += 1;
    } while (iss);
	
	for (map<string, int>::iterator i=words.begin(); i!=words.end(); ++i)
	{
		pair<string, int> word = *i;
		cout << word.first << " : " << word.second << endl;
	}
}

const string* NewsEntry::getUrl()
{
	return (const string*)&url;
}

const string* NewsEntry::getBody()
{
	return (const string*)&body;
}

map<string, int>* NewsEntry::getWords()
{
	//return (const map<string, int>* )&words;
	return &words;
}

bool NewsEntry::isBadWord(string &word)
{
	if (bad_words.find(word) == bad_words.end())
		return false;
	return true;
}

void NewsEntry::fillBadWords()
{
	bad_words.insert("a");
	bad_words.insert("an");
	bad_words.insert("the");
	bad_words.insert("this");
	bad_words.insert("that");
	bad_words.insert("them");
	bad_words.insert("their");
	bad_words.insert("it");
	bad_words.insert("should");
	bad_words.insert("they");
	bad_words.insert("will");
	bad_words.insert("would");
	bad_words.insert("was");
	bad_words.insert("is");
	bad_words.insert("will");
	bad_words.insert("were");
}

NewsEntry::~NewsEntry()
{

}
